​
<template>
  <div id="goTop">
    <div class="goTop" v-show="goTopShow" @click="goTop" :class="ismobile ? 'gom' : 'gopc'">
      <!-- icon-fanhuidingbu3 -->
      <i class="iconfont goTopIcon" :class="ismobile ? 'icon-zhiding ' : 'icon-zhiding'"></i>
      <!-- 回到顶部 -->
    </div>
  </div>
</template>
<script>
export default {
  name: "goTop",
  data() {
    return {
      ismobile: false,
      scrollTop: "",
      goTopShow: false
    };
  },
  watch: {
    scrollTop(val) {
      if (this.scrollTop > 500) {
        this.goTopShow = true;
      } else {
        this.goTopShow = false;
      }
    }
  },

  created() {
    this.ismobile = this.isMobile()
    let that = this;
    window.addEventListener('resize', function () {
      that.ismobile = that.isMobile()
    });
  },
  methods: {
    handleScroll() {
      this.scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      if (this.scrollTop > 500) {
        this.goTopShow = true;
      }
    },
    goTop() {
      let timer = null,
        _that = this;
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        if (_that.scrollTop > 0) {
          _that.scrollTop -= 250;
          document.body.scrollTop = document.documentElement.scrollTop =
            _that.scrollTop;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
          _that.goTopShow = false;
        }
      });
    }
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  }
};
</script>

<style scoped lang="scss">
.goTop {
  position: fixed;
  right: 40px;
  bottom: 60px;
  width: 43px;
  height: 43px;
  border-radius: 50%;
  z-index: 1000;
  padding: 10px;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.12);

}

.gopc {
  // background: none;
  width: 4rem;
  height: 4rem;
  // box-shadow: none !important;
  right: 21px;
  bottom: 374px;

  .goTopIcon {
    font-size: 1.5rem;
    color: rgba(51, 153, 255, 0.8);
  }
}

.gom {

  .goTopIcon {
    font-size: 1.5rem;
    color: rgba(51, 153, 255, 0.8);
  }
}

// .goTop:hover .goTopIcon {
//   font-size: 4rem;
//   color: rgba(51, 153, 255, 1);
// }
.gopc {
  // background: none;
  width: 4.3rem;
  height: 4.3rem;
  // box-shadow: none;

  .goTopIcon {
    // color: #333;
    font-size: 3rem;
  }
}
</style>

​